{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "建立Sepal.Length、Sepal.Width、Petal.Length对Petal.Width回归问题的深度神经网络预测模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "import keras\n",
    "\n",
    "# 准备基础数据\n",
    "iris = pd.read_csv(\"http://image.cador.cn/data/iris.csv\")\n",
    "x,y = iris.drop(columns=['Species','Petal.Width']),iris['Petal.Width']\n",
    "\n",
    "# 标准化处理\n",
    "x = x.apply(lambda v:(v-np.mean(v))/np.std(v))\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编写Python代码，定义网络结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义模型\n",
    "init = keras.initializers.glorot_uniform(seed=1)\n",
    "simple_adam = keras.optimizers.Adam(lr=0.0001)\n",
    "model = keras.models.Sequential()\n",
    "model.add(keras.layers.Dense(units=8, input_dim=3, kernel_initializer=init, activation='relu'))\n",
    "model.add(keras.layers.Dense(units=16, kernel_initializer=init, activation='relu'))\n",
    "model.add(keras.layers.Dense(units=8, kernel_initializer=init, activation='relu'))\n",
    "model.add(keras.layers.Dense(units=4, kernel_initializer=init, activation='relu'))\n",
    "model.add(keras.layers.Dense(units=1, kernel_initializer=init, activation='relu'))\n",
    "model.compile(loss='mean_squared_error', optimizer=simple_adam)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基于x_train和y_train对神经网络进行训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting training \n",
      "Epoch 1/100\n",
      "100/100 [==============================] - 0s 2ms/step - loss: 2.0479\n",
      "Epoch 2/100\n",
      "100/100 [==============================] - 0s 633us/step - loss: 2.0448\n",
      "Epoch 3/100\n",
      "100/100 [==============================] - 0s 553us/step - loss: 2.0407\n",
      "Epoch 4/100\n",
      "100/100 [==============================] - 0s 588us/step - loss: 2.0319\n",
      "Epoch 5/100\n",
      "100/100 [==============================] - 0s 501us/step - loss: 2.0126\n",
      "Epoch 6/100\n",
      "100/100 [==============================] - 0s 500us/step - loss: 1.9509\n",
      "Epoch 7/100\n",
      "100/100 [==============================] - 0s 486us/step - loss: 1.8671\n",
      "Epoch 8/100\n",
      "100/100 [==============================] - 0s 488us/step - loss: 1.8094\n",
      "Epoch 9/100\n",
      "100/100 [==============================] - 0s 518us/step - loss: 1.7572\n",
      "Epoch 10/100\n",
      "100/100 [==============================] - 0s 520us/step - loss: 1.7069\n",
      "Epoch 11/100\n",
      "100/100 [==============================] - 0s 450us/step - loss: 1.6594\n",
      "Epoch 12/100\n",
      "100/100 [==============================] - 0s 487us/step - loss: 1.6102\n",
      "Epoch 13/100\n",
      "100/100 [==============================] - 0s 518us/step - loss: 1.5610\n",
      "Epoch 14/100\n",
      "100/100 [==============================] - 0s 453us/step - loss: 1.5117\n",
      "Epoch 15/100\n",
      "100/100 [==============================] - 0s 474us/step - loss: 1.4596\n",
      "Epoch 16/100\n",
      "100/100 [==============================] - 0s 467us/step - loss: 1.4052\n",
      "Epoch 17/100\n",
      "100/100 [==============================] - 0s 453us/step - loss: 1.3479\n",
      "Epoch 18/100\n",
      "100/100 [==============================] - 0s 450us/step - loss: 1.2856\n",
      "Epoch 19/100\n",
      "100/100 [==============================] - 0s 443us/step - loss: 1.2208\n",
      "Epoch 20/100\n",
      "100/100 [==============================] - 0s 459us/step - loss: 1.1524\n",
      "Epoch 21/100\n",
      "100/100 [==============================] - 0s 470us/step - loss: 1.0817\n",
      "Epoch 22/100\n",
      "100/100 [==============================] - 0s 465us/step - loss: 1.0061\n",
      "Epoch 23/100\n",
      "100/100 [==============================] - 0s 473us/step - loss: 0.9285\n",
      "Epoch 24/100\n",
      "100/100 [==============================] - 0s 439us/step - loss: 0.8480\n",
      "Epoch 25/100\n",
      "100/100 [==============================] - 0s 608us/step - loss: 0.7677\n",
      "Epoch 26/100\n",
      "100/100 [==============================] - 0s 463us/step - loss: 0.6865\n",
      "Epoch 27/100\n",
      "100/100 [==============================] - 0s 489us/step - loss: 0.6070\n",
      "Epoch 28/100\n",
      "100/100 [==============================] - 0s 489us/step - loss: 0.5294\n",
      "Epoch 29/100\n",
      "100/100 [==============================] - 0s 478us/step - loss: 0.4554\n",
      "Epoch 30/100\n",
      "100/100 [==============================] - 0s 478us/step - loss: 0.3882\n",
      "Epoch 31/100\n",
      "100/100 [==============================] - 0s 512us/step - loss: 0.3274\n",
      "Epoch 32/100\n",
      "100/100 [==============================] - 0s 507us/step - loss: 0.2738\n",
      "Epoch 33/100\n",
      "100/100 [==============================] - 0s 565us/step - loss: 0.2281\n",
      "Epoch 34/100\n",
      "100/100 [==============================] - 0s 506us/step - loss: 0.1913\n",
      "Epoch 35/100\n",
      "100/100 [==============================] - 0s 515us/step - loss: 0.1608\n",
      "Epoch 36/100\n",
      "100/100 [==============================] - 0s 557us/step - loss: 0.1392\n",
      "Epoch 37/100\n",
      "100/100 [==============================] - 0s 508us/step - loss: 0.1210\n",
      "Epoch 38/100\n",
      "100/100 [==============================] - 0s 507us/step - loss: 0.1080\n",
      "Epoch 39/100\n",
      "100/100 [==============================] - 0s 487us/step - loss: 0.0981\n",
      "Epoch 40/100\n",
      "100/100 [==============================] - 0s 510us/step - loss: 0.0912\n",
      "Epoch 41/100\n",
      "100/100 [==============================] - 0s 484us/step - loss: 0.0858\n",
      "Epoch 42/100\n",
      "100/100 [==============================] - 0s 507us/step - loss: 0.0817\n",
      "Epoch 43/100\n",
      "100/100 [==============================] - 0s 534us/step - loss: 0.0785\n",
      "Epoch 44/100\n",
      "100/100 [==============================] - 0s 485us/step - loss: 0.0757\n",
      "Epoch 45/100\n",
      "100/100 [==============================] - 0s 475us/step - loss: 0.0738\n",
      "Epoch 46/100\n",
      "100/100 [==============================] - 0s 488us/step - loss: 0.0719\n",
      "Epoch 47/100\n",
      "100/100 [==============================] - 0s 513us/step - loss: 0.0708\n",
      "Epoch 48/100\n",
      "100/100 [==============================] - 0s 494us/step - loss: 0.0693\n",
      "Epoch 49/100\n",
      "100/100 [==============================] - 0s 507us/step - loss: 0.0685\n",
      "Epoch 50/100\n",
      "100/100 [==============================] - 0s 561us/step - loss: 0.0674\n",
      "Epoch 51/100\n",
      "100/100 [==============================] - 0s 496us/step - loss: 0.0665\n",
      "Epoch 52/100\n",
      "100/100 [==============================] - 0s 626us/step - loss: 0.0653\n",
      "Epoch 53/100\n",
      "100/100 [==============================] - 0s 563us/step - loss: 0.0643\n",
      "Epoch 54/100\n",
      "100/100 [==============================] - 0s 621us/step - loss: 0.0636\n",
      "Epoch 55/100\n",
      "100/100 [==============================] - 0s 510us/step - loss: 0.0627\n",
      "Epoch 56/100\n",
      "100/100 [==============================] - 0s 502us/step - loss: 0.0621\n",
      "Epoch 57/100\n",
      "100/100 [==============================] - 0s 508us/step - loss: 0.0615\n",
      "Epoch 58/100\n",
      "100/100 [==============================] - 0s 509us/step - loss: 0.0609\n",
      "Epoch 59/100\n",
      "100/100 [==============================] - 0s 515us/step - loss: 0.0604\n",
      "Epoch 60/100\n",
      "100/100 [==============================] - 0s 600us/step - loss: 0.0599\n",
      "Epoch 61/100\n",
      "100/100 [==============================] - 0s 603us/step - loss: 0.0593\n",
      "Epoch 62/100\n",
      "100/100 [==============================] - 0s 572us/step - loss: 0.0586\n",
      "Epoch 63/100\n",
      "100/100 [==============================] - 0s 571us/step - loss: 0.0582\n",
      "Epoch 64/100\n",
      "100/100 [==============================] - 0s 571us/step - loss: 0.0578\n",
      "Epoch 65/100\n",
      "100/100 [==============================] - 0s 758us/step - loss: 0.0569\n",
      "Epoch 66/100\n",
      "100/100 [==============================] - 0s 670us/step - loss: 0.0565\n",
      "Epoch 67/100\n",
      "100/100 [==============================] - 0s 585us/step - loss: 0.0564\n",
      "Epoch 68/100\n",
      "100/100 [==============================] - 0s 625us/step - loss: 0.0558\n",
      "Epoch 69/100\n",
      "100/100 [==============================] - 0s 683us/step - loss: 0.0550\n",
      "Epoch 70/100\n",
      "100/100 [==============================] - 0s 526us/step - loss: 0.0553\n",
      "Epoch 71/100\n",
      "100/100 [==============================] - 0s 567us/step - loss: 0.0541\n",
      "Epoch 72/100\n",
      "100/100 [==============================] - 0s 560us/step - loss: 0.0536\n",
      "Epoch 73/100\n",
      "100/100 [==============================] - 0s 556us/step - loss: 0.0533\n",
      "Epoch 74/100\n",
      "100/100 [==============================] - 0s 581us/step - loss: 0.0527\n",
      "Epoch 75/100\n",
      "100/100 [==============================] - 0s 583us/step - loss: 0.0521\n",
      "Epoch 76/100\n",
      "100/100 [==============================] - 0s 620us/step - loss: 0.0515\n",
      "Epoch 77/100\n",
      "100/100 [==============================] - 0s 599us/step - loss: 0.0512\n",
      "Epoch 78/100\n",
      "100/100 [==============================] - 0s 676us/step - loss: 0.0506\n",
      "Epoch 79/100\n",
      "100/100 [==============================] - 0s 746us/step - loss: 0.0504\n",
      "Epoch 80/100\n",
      "100/100 [==============================] - 0s 584us/step - loss: 0.0497\n",
      "Epoch 81/100\n",
      "100/100 [==============================] - 0s 561us/step - loss: 0.0495\n",
      "Epoch 82/100\n",
      "100/100 [==============================] - 0s 609us/step - loss: 0.0488\n",
      "Epoch 83/100\n",
      "100/100 [==============================] - 0s 663us/step - loss: 0.0483\n",
      "Epoch 84/100\n",
      "100/100 [==============================] - 0s 698us/step - loss: 0.0481\n",
      "Epoch 85/100\n",
      "100/100 [==============================] - 0s 812us/step - loss: 0.0476\n",
      "Epoch 86/100\n",
      "100/100 [==============================] - 0s 740us/step - loss: 0.0480\n",
      "Epoch 87/100\n",
      "100/100 [==============================] - 0s 648us/step - loss: 0.0471\n",
      "Epoch 88/100\n",
      "100/100 [==============================] - 0s 625us/step - loss: 0.0468\n",
      "Epoch 89/100\n",
      "100/100 [==============================] - 0s 583us/step - loss: 0.0464\n",
      "Epoch 90/100\n",
      "100/100 [==============================] - 0s 620us/step - loss: 0.0463\n",
      "Epoch 91/100\n",
      "100/100 [==============================] - 0s 535us/step - loss: 0.0457\n",
      "Epoch 92/100\n",
      "100/100 [==============================] - 0s 573us/step - loss: 0.0452\n",
      "Epoch 93/100\n",
      "100/100 [==============================] - 0s 626us/step - loss: 0.0452\n",
      "Epoch 94/100\n",
      "100/100 [==============================] - 0s 696us/step - loss: 0.0447\n",
      "Epoch 95/100\n",
      "100/100 [==============================] - 0s 636us/step - loss: 0.0445\n",
      "Epoch 96/100\n",
      "100/100 [==============================] - 0s 507us/step - loss: 0.0442\n",
      "Epoch 97/100\n",
      "100/100 [==============================] - 0s 508us/step - loss: 0.0439\n",
      "Epoch 98/100\n",
      "100/100 [==============================] - 0s 513us/step - loss: 0.0438\n",
      "Epoch 99/100\n",
      "100/100 [==============================] - 0s 776us/step - loss: 0.0432\n",
      "Epoch 100/100\n",
      "100/100 [==============================] - 0s 658us/step - loss: 0.0430\n",
      "Training finished \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 训练模型\n",
    "b_size = 2\n",
    "max_epochs = 100\n",
    "print(\"Starting training \")\n",
    "h = model.fit(x_train, y_train, batch_size=b_size, epochs=max_epochs, shuffle=True, verbose=1)\n",
    "print(\"Training finished \\n\")\n",
    "\n",
    "# Starting training \n",
    "# Epoch 1/100\n",
    "# 100/100 [==============================] - 2s 20ms/step - loss: 2.0479\n",
    "# ......\n",
    "# Epoch 99/100\n",
    "# 100/100 [==============================] - 0s 1ms/step - loss: 0.0429\n",
    "# Epoch 100/100\n",
    "# 100/100 [==============================] - 0s 1ms/step - loss: 0.0428\n",
    "# Training finished "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对x_test进行预测，并与y_test进行比较，计算残差平方和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Evaluation on test data: loss = 1.871250 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 评估模型\n",
    "out = model.evaluate(x_test, y_test, verbose=0)\n",
    "print(\"Evaluation on test data: loss = %0.6f \\n\" % (out*len(y_test)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
